<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use App\Models\StockActivity;
use App\Models\StockReward;
use App\Repositories\MemberRepositoryEloquent;

class RewardsCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'rewards:settlement';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '对对乐定时任务';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct(MemberRepositoryEloquent $user)
    {
        parent::__construct();
        $this->user = $user;
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //查询开始时间大于开始当前时间并且状态为等待中(0)的活动
        $y=date("Y",time()); 
        $m=date("m",time()); 
        $d=date("d",time()); 
        $end_time = mktime(15, 05, 0, $m, $d ,$y); 
        $activities = StockActivity::where('end_time','>=',$end_time)->where('end_time','<',time())->where('status','=',2)->get();
        //$activities = StockActivity::where('status','=',2)->get();
        if(!$activities){
            return;
        } 
        //循环活动
        foreach($activities as $activity){
            
            //$orders = $activity->orders()->where('status',1)->where('open_price','=',0)->get();
            $orders = $activity->orders()->where('status',1)->get();
            //生成记录
            $orders = $orders->sortByDesc("yield")->values();
            $grouped = $orders->groupBy('yield');
            //dd($grouped);
            //$grouped = $grouped->toArray();
            $total_count = $orders->count();
            $total_money = $orders->sum('pay_money');
            $first = $grouped->shift();  //第一名
            if(empty($first)){
                continue;
            } 
            //dd($first);
            $first_count = $first->count();
            if($first_count>=3 || $first_count==$total_count){
                //100%奖金平静分配
                $money = sprintf("%.2f",$total_money/$first_count);
                foreach($first as $k=>$f_w){
                    $this->rewards($f_w,$money,1);
                }
                continue;
            }elseif($first_count==2){
                $money = sprintf("%.2f",$total_money*0.9/$first_count);
                foreach($first as $k=>$f_w){
                    $this->rewards($f_w,$money,1);
                }
                
            }else{
                $money = sprintf("%.2f",$total_money*0.6/$first_count);
                foreach($first as $k=>$f_w){
                    $this->rewards($f_w,$money,1);
                }

            }
            
            $second = $grouped->shift();  //第二名
            if(empty($second)){
                continue;
            } 
            $second_count = $second->count();
            if(($first_count+$second_count)>=3 || ($first_count+$second_count)==$total_count){
                if($first_count==2){
                    //100%奖金平静分配
                    $money = sprintf("%.2f",$total_money*0.1/$second_count);
                    foreach($second as $k=>$s_w){
                        $this->rewards($s_w,$money,2);
                    }
                }else{
                    //100%奖金平静分配
                    $money = sprintf("%.2f",$total_money*0.4/$second_count);
                    foreach($second as $k=>$s_w){
                        $this->rewards($s_w,$money,2);
                    }
                }
               
                continue;
            }else{
                $money = sprintf("%.2f",$total_money*0.3/$second_count);
                foreach($second as $k=>$s_w){
                    $this->rewards($s_w,$money,2);
                }

            }

            $third = $grouped->shift();  //第三名
            if(empty($third)){
                continue;
            } 
            $third_count = $third->count();
           
            //10%奖金平静分配
            $money = sprintf("%.2f",$total_money*0.1/$third_count);
            foreach($third as $k=>$t_w){
                $this->rewards($t_w,$money,3);
            }
            continue;
            

            
        }
    
    }



    public function rewards($order,$money,$level){
        $info = StockReward::where('stock_activity_id','=',$order->stock_activity_id)
                    ->where('user_id','=',$order->user_id)
                    ->where('stock_order_id','=',$order->stock_order_id)->first();
        if($info){
            return null;
        }
        $data = null;
        $data['stock_activity_id'] = $order->stock_activity_id;
        $data['user_id'] = $order->user_id;
        $data['stock_order_id'] = $order->id;
        $data['money'] = $money;
        $data['level'] = 1;
        $data['partner_trade_no'] = date('YmdHis').$order->id.rand(100,999);
        $data['payment_time'] = date('Y-m-d H:i:s');
        $data['note'] = $order->activity->name.'第'.$level.'名奖金['.$order->out_trade_no.']';
        if(StockReward::create($data)){
            $this->user->modifyBalance($order->user_id,$money,6,$data['note']);
        }
    }
}
